//
//  VersonUpdateView.m
//  driftbottle
//
//  Created by wujian on 2017/5/18.
//  Copyright © 2017年 wujian. All rights reserved.
//

#import "VersonUpdateView.h"

@implementation VersonUpdateView

-(id)initWithFrame:(CGRect)frame{
    
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        /**
         * 透明背景
         **/
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = CSBlackColor;
        alphaView.alpha = 0.4;
        [self addSubview:alphaView];
        self.alphaView = alphaView;
        
        /**
         * 白色
         **/
        UIImageView *bgWhiteView = [[UIImageView alloc] init];
        bgWhiteView.image = [UIImage imageNamed:@"pop_version_upgrade"];
        [self addSubview:bgWhiteView];
        self.bgWhiteView = bgWhiteView;
        
        /**
         * TitleLabel
         **/
        TTTAttributedLabel *titleLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        titleLabel.text = @"重要版本升级";
        titleLabel.font = [UIFont systemFontOfSize:16.0];
        titleLabel.numberOfLines = 0;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
        titleLabel.lineSpacing = 5;
        titleLabel.textColor = CSColorFromRGB(0x222222);
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        /**
         * 提示Label
         **/
        TTTAttributedLabel *noticeLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        noticeLabel.font = [UIFont systemFontOfSize:12.0];
        noticeLabel.numberOfLines = 0;
        //noticeLabel.textAlignment = NSTextAlignmentCenter;
        noticeLabel.lineBreakMode = NSLineBreakByCharWrapping;
        noticeLabel.lineSpacing = 5;
        noticeLabel.textColor = CSColorFromRGB(0x999999);
        noticeLabel.text = @"1、修改已知BUG\n2、优化界面\n3、提高程序稳定性";
        [self addSubview:noticeLabel];
        self.noticeLabel = noticeLabel;
        
        
        /**
         * 线条2
         **/
        UIButton *sureBtn = [[UIButton alloc] init];
        [sureBtn setTitle:@"立即\r\n升级"];
        sureBtn.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
        sureBtn.titleLabel.numberOfLines = 0;
        [sureBtn setTitleColor:CSColorFromRGB(0x5D553F)];
        [sureBtn setBgImage:@"pop_version_upgrade_btn"];
        sureBtn.titleLabel.font = MidFont;
        [self addSubview:sureBtn];
        self.sureBtn = sureBtn;
        
        [self setConstraints];
    }
    
    return self;
}


/**
 * 设置约束
 **/
-(void)setConstraints{
    
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    [self.bgWhiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.center.mas_equalTo(self);
        
//        make.left.mas_equalTo(self).offset(30);
//
//        make.right.mas_equalTo(self).offset(-30);
        
    }];
    
    /**
     * TitleLabel
     **/
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.bgWhiteView).offset(30);
        
        make.centerX.mas_equalTo(self.bgWhiteView);
        
        make.height.mas_equalTo(20);
    }];
        
    [self.noticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(15);
        
        make.left.mas_equalTo(self.bgWhiteView).offset(40);
        
        make.right.mas_equalTo(self.bgWhiteView).offset(-40);
        
        make.bottom.mas_equalTo(self.sureBtn.mas_top).offset(-10);
    }];
    
    
    
    [self.sureBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self.bgWhiteView);
        
        make.bottom.mas_equalTo(self.bgWhiteView).offset(-70);
        
        make.width.height.mas_equalTo(90);
    }];
    
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    [self removeFromSuperview];
}


/**
 显示页面
 */
- (void)showWithAnimation{
    
    [self addViews];
    
    self.bgWhiteView.transform = CGAffineTransformScale(CGAffineTransformIdentity, CGFLOAT_MIN, CGFLOAT_MIN);
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.bgWhiteView.transform = CGAffineTransformIdentity;
        
    }];
}


/**
 隐藏页面
 */
- (void)hiddenWithAnimation {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.bgWhiteView.transform = CGAffineTransformScale( CGAffineTransformIdentity, 0.001, 0.001);
        
    } completion:^(BOOL finished) {
        
        [self hiddenViews];
    }];
}

@end
